/****************************************************************************
* COSC1283/1284 - Programming Techniques
* Semester 1 2011 Assignment #2 - Gloria Jean's Coffee system
* Full Name        : LE YAN 
* Student Number   : 3262302
* Yallara Username : s3262302
* Course Code      : BP232 
* Program Code     : COSC1284
* Start up code provided by Christopher Hoobin and Xiaodong Li
****************************************************************************/

#include "gjc.h"
#include "gjc_options.h"
#include "gjc_utility.h"

#define usage "usage: gjc <menufile> <submenufile>"
#define NO 0
#define YES 1
int 
main(int argc, char* argv[])
{
   GJCType gjc;
   int quit = NO;
   int initFlag, dataFlag, i;
   FILE *menufile, *submenufile;
   char input, tmp;

   char* menu[9] = { 
                     "(1) Hot Drinks Summary\n",
                     "(2) Cold Drinks Summary\n",
                     "(3) Detailed Menu Report\n",
                     "(4) Add Menu Category\n",
                     "(5) Delete Menu Category\n",
                     "(6) Add Menu Item\n",
                     "(7) Delete Menu Item\n",
                     "(8) Save & Exit\n",
                     "(9) Abort\n"
                   };

   if( argc < 3 )
   {
     fprintf(stderr, "Too few arguments\n");
     fprintf(stderr, "%s\n", usage);
     exit(-1);
   }

   menufile = fopen(argv[1], "r");
   submenufile = fopen(argv[2], "r");
  
   /* if fail to open either file */
   if( menufile == NULL || submenufile == NULL )
   {
     fprintf(stderr, "Fail to open menfuile file or submenufile\n");
     exit(-1);
   }
   fclose(menufile);
   fclose(submenufile);

   /* Initialise the Gloria Jean's Coffee system to a safe empty state. */
   initFlag = systemInit(&gjc);

   /* Populate the Gloria Jean's Coffee system with data from the data files. */
   /* Uncomment this line when you are ready to use command line arguments: */
   dataFlag = loadData(&gjc, argv[1], argv[2]); 

   /* Testing to see if both systemInit(.) and loadData(.) are ok */
   if (initFlag == FAILURE || dataFlag == FAILURE)
   {
     fprintf(stderr,"Init or Load Data file error\n");
     exit(EXIT_FAILURE);
   }

   /* Interactive menu providing user with access to the 9 menu options */
   while(1)
   {
     /* print menu */
     printf("Main Menu\n");
     for(i=0; i<9; i++)
     {
      printf("%s", menu[i]);
     }
     printf("\n");
     printf("Select your option (1-9): ");
    

     input = getc(stdin); 
     if( input == '\n')
      continue;
     
     if( (tmp=getc(stdin)) != '\n' )
     {
       readRestOfLine();
       fprintf(stderr, "You input is invalid!\n\n");
       continue;
     }
     else
     {
       ungetc(tmp, stdin);
     }
     
     readRestOfLine();
     switch (input)
     {
        case '1':
                displaySummary(&gjc, 'H');
                break;
        case '2':
                displaySummary(&gjc, 'C');
                break;
        case '3':
                categoryReport(&gjc);
                break;
        case '4':
                addCategory(&gjc); 
                break;
        case '5':
                deleteCategory(&gjc); 
                break;
        case '6':
                addItem(&gjc);  
                break;
        case '7':
                deleteItem(&gjc); 
                break;

        case '8':
                saveData( &gjc, argv[1], argv[2] );  
                quit = YES;
                break;

        case '9':
                quit = YES;
                break;

        default: 
                fprintf(stderr, "You input is invalid!\n\n");
                continue;
     };

     if( quit == YES )
        break;

   }


   /* Deallocate all dynamically allocated memory. */
   systemFree(&gjc);

   exit(EXIT_SUCCESS);

}
